<!--
  作者：xhr
  时间：2022年09月02日 21:40:14
-->

<template>
    <div style="padding: 10px">
        <div style="margin: 10px 0">
            <el-button style="margin-right: 10px;" type="primary" @click="add">新增</el-button>
            <el-popconfirm style="margin-right: 10px;" class="ml-5" confirm-button-text='好的' cancel-button-text='不用了'
                icon="el-icon-info" icon-color="red" title="您确定删除吗？" @confirm="delBath">
                <el-button plain class="el-icon-circle-close" type="danger" slot="reference">批量删除</el-button>
            </el-popconfirm>
            <el-input v-model="search" placeholder="搜索题目名" style="width: 20%" clearable @clear="reply"></el-input>
            <el-button type="primary" style="margin: 0 10px 0 5px" @click="loading">查询</el-button>
            <el-select v-model="value" filterable clearable @clear="reply" placeholder="课程筛选"
                :filter-method="selectCourse(value)">
                <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
            </el-select>
        </div>
        <el-table :data="tableData" border stripe style="width: calc(98%)" max-height="630">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column sortable prop="id" label="ID"></el-table-column>
            <el-table-column prop="name" label="名称"></el-table-column>
            <el-table-column prop="type" label="类型">
                <template v-slot="scope">
                    <span v-if="scope.row.type == 1">选择题</span>
                    <span v-if="scope.row.type == 2">判断题</span>
                    <span v-if="scope.row.type == 3">简答题</span>
                </template>
            </el-table-column>
            <el-table-column label="所属课程">
                <template slot-scope="scope">
                    <span>{{ courses.find(v => v.id === scope.row.courseId) ? courses.find(v =>
                        v.id === scope.row.courseId).name : '' }}</span>
                </template>
            </el-table-column>
            <el-table-column prop="a" label="A选项"></el-table-column>
            <el-table-column prop="b" label="B选项"></el-table-column>
            <el-table-column prop="c" label="C选项"></el-table-column>
            <el-table-column prop="d" label="D选项"></el-table-column>
            <el-table-column prop="score" label="分数"></el-table-column>
            <el-table-column prop="answear" label="答案"></el-table-column>
            <el-table-column prop="detail" label="解析"></el-table-column>
            <el-table-column prop="userName" label="出题人"></el-table-column>
            <el-table-column prop="time" label="出题时间" width="150"></el-table-column>
            <el-table-column fixed="right" label="操作" width="150">
                <template slot-scope="scope">
                    <!-- <el-button @click="details(scope.row)" size="mini">详情</el-button> -->
                    <el-button @click="update(scope.row)" size="mini">编辑</el-button>
                    <el-popconfirm title="确定删除吗？" style="margin-left: 5px" @confirm="delate(scope.row.id)">
                        <el-button type="danger" size="mini" slot="reference">删除
                        </el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <div style="margin: 10px 0">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage"
                :page-sizes="[5, 10, 50]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </div>
        <el-dialog title="信息" :visible.sync="dialogVisible" width="30%">
            <el-form label-width="120px" :model="form">
                <el-form-item label="类型">
                    <el-select v-model="form.type" placeholder="请选择" clearable style="width: 80%">
                        <el-option
                            v-for="item in [{ value: 1, label: '选择题' }, { value: 2, label: '判断题' }, { value: 3, label: '简答题' }]"
                            :key="item.value" :label="item.label" :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="课程">
                    <el-select v-model="form.courseId" placeholder="请选择" clearable style="width: 80%">
                        <el-option v-for="item in courses" :key="item.id" :label="item.name" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="名称">
                    <el-input v-model="form.name" style="width: 80%"></el-input>
                </el-form-item>

                <el-form-item label="选项a" v-show="form.type == 1">
                    <el-input v-model="form.a" style="width: 80%"></el-input>
                </el-form-item>
                <el-form-item label="选项b" v-show="form.type == 1">
                    <el-input v-model="form.b" style="width: 80%"></el-input>
                </el-form-item>
                <el-form-item label="选项c" v-show="form.type == 1">
                    <el-input v-model="form.c" style="width: 80%"></el-input>
                </el-form-item>
                <el-form-item label="选项d" v-show="form.type == 1">
                    <el-input v-model="form.d" style="width: 80%"></el-input>
                </el-form-item>
                <el-form-item label="分数">
                    <el-input v-model="form.score" style="width: 80%"></el-input>
                </el-form-item>
                <el-form-item label="答案" v-if="form.type == 1">
                    <el-radio v-model="form.answear" label="A">A</el-radio>
                    <el-radio v-model="form.answear" label="B">B</el-radio>
                    <el-radio v-model="form.answear" label="C">C</el-radio>
                    <el-radio v-model="form.answear" label="D">D</el-radio>
                </el-form-item>
                <el-form-item label="答案" v-if="form.type == 2">
                    <el-radio v-model="form.answear" label="是">是</el-radio>
                    <el-radio v-model="form.answear" label="否">否</el-radio>
                </el-form-item>
                <el-form-item label="答案" v-if="form.type == 3">
                    <el-input type="textarea" v-model="form.answear" style="width: 80%"></el-input>
                </el-form-item>
                <el-form-item label="解析">
                    <el-input type="textarea" v-model="form.detail" style="width: 80%"></el-input>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
            </span>
        </el-dialog>
        <el-dialog title="详情" :visible.sync="vis" width="50%">
            <el-card>
                <div v-html="detail.content" style="min-height: 100px"></div>
            </el-card>
        </el-dialog>
    </div>
</template>

<script>
import request from "@/utils/request";



export default {
    name: "question",
    data() {
        return {
            vis: false,
            form: {},
            total: 0,
            currentPage: 1,
            pageSize: 10,
            search: "",
            dialogVisible: false,
            tableData: [],
            detail: {},
            courses: [],
            options: [],
            value: '',
            courseId: null,
        };
    },
    created() {
        request.get("/course").then((res) => {
            this.courses = res.data;
            this.courses.forEach(e => {
                var obj = {};
                obj.value = e.id;
                obj.label = e.name;
                this.options.push(obj);
            });
            // console.log(this.options);
        });
        this.loading();

    },
    mounted() { },
    methods: {
        filesUploadSuccess(res) {
            this.form.cover = res.data;
        },
        loading() {
            request
                .get("/question/page", {
                    params: {
                        pagenum: this.currentPage,
                        pagesize: this.pageSize,
                        search: this.search,
                        courseId: this.courseId,
                    },
                })
                .then((res) => {
                    this.tableData = res.data.records;
                    this.total = res.data.total;
                });
        },
        save() {


            if (this.form.id) {
                request.put("/question", this.form).then((res) => {

                    if (res.code === "0") {
                        this.$message({
                            type: "success",
                            message: "更新成功",
                        });
                    } else {
                        this.$message({
                            type: "error",
                            message: "更新失败",
                        });
                    }
                    this.loading();
                    this.dialogVisible = false;

                });
            } else {
                //出题人
                let userStr = sessionStorage.getItem("user") || "{}";
                let user = JSON.parse(userStr);
                this.form.userName = user.nickName;
                //出题人
                request.post("/question", this.form).then((res) => {
                    console.log(res);
                    if (res.code === "0") {
                        this.$message({
                            type: "success",
                            message: "新增成功",
                        });
                    } else {
                        this.$message({
                            type: "error",
                            message: "新增失败",
                        });
                    }
                    this.loading();
                    this.dialogVisible = false;

                });
            }
        },
        add() {
            this.dialogVisible = true;
            this.form = {};


        },
        update(row) {
            this.form = JSON.parse(JSON.stringify(row));
            this.dialogVisible = true;


        },
        delate(id) {
            request.delete("/question/" + id).then((res) => {
                if (res.code === "0") {
                    this.$message({
                        type: "success",
                        message: "删除成功",
                    });
                } else {
                    this.$message({
                        type: "error",
                        message: "删除失败",
                    });
                }
            });
            this.loading();
        },
        handleSizeChange(pageSize) {
            this.pageSize = pageSize;
            this.loading();
        },
        handleCurrentChange(pageNum) {
            this.currentPage = pageNum;
            this.loading();
        },
        // reply() {
        //     this.loading();
        // },
        delBath() {
            // if (!this.ids.length) {
            //   this.$message.warning("请选择数据！")
            //   return
            // }
            // request.delete("/activity/delBatch/" + this.ids).then(res => {
            //   this.$message.success("批量删除成功！");
            //   this.loading()
            // })
        },
        selectCourse(st) {
            if (st != "") {
                this.courseId = st;
                this.loading();
            }
        },
        reply() {
            this.courseId = null;
            this.loading();
        },
    },
};
</script>

<style></style>